package com.hemerasolutions.v2trainer.component.table;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.table.RoleComponentInterface;
import com.hemerasolutions.v2trainer.dao.interfaces.RoleDaoInterface;
import com.hemerasolutions.v2trainer.model.security.AccessRole;

@Component("roleComp")
@Scope("request")
public class RoleComponent extends BaseComponent implements RoleComponentInterface  {

	private static final long serialVersionUID = 1L;

	@Autowired
	public RoleDaoInterface roleDao;
	
	public List<AccessRole> roles;
	
	public List<AccessRole> fullRoles;
	
	public AccessRole selRole;
	
	public AccessRole role;
	
	@PostConstruct
	public void init(){
		this.loadAvailableRoles();
	}
	
	public void loadAvailableRoles() {
		
		List<AccessRole> baseRoles = new ArrayList<AccessRole>();
		fullRoles = new ArrayList<AccessRole>();
		
		baseRoles = roleDao.list();

		if(this.getContextRoles() != null){
			for(AccessRole role: this.getContextRoles()){
				baseRoles.remove(role);
			}
		}
		
		for(AccessRole role:baseRoles){
			if(	role.getName().equals("ROLE_ADMIN") 	||
				role.getName().equals("ROLE_ACADEMY") 	||
				role.getName().equals("ROLE_ATHLETE") 	||
				role.getName().equals("ROLE_PERSONAL") 	||
				role.getName().equals("ROLE_TEACHER")){
			}else{
				fullRoles.add(role);
			}
		}

	}

	public void addRole(){
		this.roles = this.getContextRoles();
		
		roles.add(this.selRole);

		this.setContextRoles(roles);

		this.loadAvailableRoles();
		
	}

	public void deleteRole(){
		this.roles = this.getContextRoles();
		
		roles.remove(this.role);

		this.setContextRoles(roles);
		
	}
	
	
	public void viewRole(){

	}

	@SuppressWarnings("unchecked")
	public List<AccessRole> getContextRoles(){
		return (List<AccessRole>)this.getCCValue("roles", List.class);
	}
	
	
	public void setContextRoles(List<AccessRole> roles){
		this.setCCValue("roles", roles, List.class);
	}

	public List<AccessRole> getRoles() {
		return roles;
	}

	public List<AccessRole> getFullRoles() {
		return fullRoles;
	}

	public AccessRole getSelRole() {
		return selRole;
	}

	public AccessRole getRole() {
		return role;
	}

	public void setRoles(List<AccessRole> roles) {
		this.roles = roles;
	}

	public void setFullRoles(List<AccessRole> fullRoles) {
		this.fullRoles = fullRoles;
	}

	public void setSelRole(AccessRole selRole) {
		this.selRole = selRole;
	}

	public void setRole(AccessRole role) {
		this.role = role;
	}
	
	
	
}
